﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoLoL.Entities;
using System.Xml.Linq;
using System.Collections.ObjectModel;
using System.IO;
using AutoLoL.Shared;
using AutoLoL.Logic.Utils;

namespace AutoLoL.Logic
{
    public class ChampionRepository
    {
        private static ReadOnlyCollection<Champion> _champions;

        public static Champion GetByName(string championName)
        {
            if (string.IsNullOrEmpty(championName))
            {
                return null;
            }

            return GetAll().Where(c => c.Name.ToLower().Equals(championName.ToLower())).SingleOrDefault();
        }

        public static ReadOnlyCollection<Champion> GetAll(bool cached = true)
        {
            if (_champions == null || !cached)
            {
                List<Champion> champions = new List<Champion>();

                if (File.Exists(Constants.CHAMPIONS_XML_FILE))
                {
                    XDocument championsDoc = XDocument.Load(Constants.CHAMPIONS_XML_FILE);
                    foreach (XElement championElement in championsDoc.Root.Elements("champion"))
                    {
                        string directory = championElement.Element("directory").Value;

                        string name = championElement.Element("name").Value;
                        string icon = championElement.Element("icon").Value;
                        string description = championElement.Element("description").Value;
                        string lore = championElement.Element("lore").Value;

                        string championDirectory = Path.Combine(Constants.CHAMPIONS_DIRECTORY, directory);
                        string iconPath = Path.Combine(championDirectory, icon);

                        Champion champion = new Champion();
                        champion.Name = name;
                        champion.IconBitmapImage = ImageConverter.ConvertFileToBitmapImage(iconPath);
                        champion.Description = description;
                        champion.Lore = lore;

                        List<Skin> skins = new List<Skin>();
                        if (championElement.Element("skins") != null)
                        {
                            foreach (XElement skinElement in championElement.Element("skins").Descendants())
                            {
                                string skinName = skinElement.Attribute("name").Value;
                                string obj = skinElement.Attribute("obj").Value;
                                string jpeg = skinElement.Attribute("jpeg").Value;

                                string objPath = Path.Combine(championDirectory, obj);
                                string jpegPath = Path.Combine(championDirectory, jpeg);

                                Skin skin = new Skin(skinName, objPath, jpegPath);
                                skins.Add(skin);
                            }
                        }

                        champion.Skins = skins;
                        champions.Add(champion);
                    }
                }

                champions.Sort((x1, x2) => x1.Name.CompareTo(x2.Name));
                _champions = new ReadOnlyCollection<Champion>(champions);
            }

            return _champions;
        }
    }
}